package m09;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;

import m05.ConfigScreen;
import m06.SolarSystem;
import m07.Player;


public class WindowFrame extends JFrame implements Serializable {
	private static final long serialVersionUID = 5708824225797706599L;

	private JPanel contentPane;
	
	private static ConfigScreen configFrame = new ConfigScreen();
	private static SolarSystem solarSystem = new SolarSystem();
	private static WindowFrame welPanel = new WindowFrame();

	/**
	 * Launches the application.
	 * 
	 * @param the standard command line parameters.
	 */
	public static void main(String[] args) {
		
		// Creates and prints out the Universe to the Console
		solarSystem.activateSolarSystem();
		
		welPanel.setVisible(true);
	} 

	/**
	 * Create the frame.
	 */
	public WindowFrame() {
		setAlwaysOnTop(true);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 324, 207);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblWelcomeToSpace = new JLabel("Welcome to Space Trader!");
		lblWelcomeToSpace.setBounds(78, 45, 167, 16);
		
		JButton btnNewGame = new JButton("New Game");
		btnNewGame.setBounds(22, 120, 117, 29);
		contentPane.add(btnNewGame);
		btnNewGame.addActionListener(new NewListener());
		
		JButton btnSavedGame = new JButton("Saved Game");
		btnSavedGame.setBounds(173, 120, 117, 29);
		contentPane.add(btnSavedGame);
		btnSavedGame.addActionListener(new SaveListener());
		
		BufferedImage myPicture;
		try {
			ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
			InputStream input = classLoader.getResourceAsStream("wel_screen.png");
			myPicture = ImageIO.read(input);
			JLabel picLabel = new JLabel(new ImageIcon(myPicture));
			picLabel.setBounds(0, 0, 324, 207);
			contentPane.add(picLabel);
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Background Not Loaded");
		}
	}
	
	private class SaveListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			welPanel.setVisible(false);

			try {
				FileInputStream fileIn = new FileInputStream("gameList.ser");
	            ObjectInputStream in = new ObjectInputStream(fileIn);
	            @SuppressWarnings("unchecked")
				ArrayList<Player> myPlayerList = (ArrayList<Player>) in.readObject();
	            in.close();
	            fileIn.close();
	            
	            for (Player p : myPlayerList){
	            	System.out.println(p.getName());
	            }
	            System.out.println(myPlayerList);
	            LoadSavedGame lsg = new LoadSavedGame(myPlayerList);
	            lsg.setVisible(true);
			}
			catch (Exception e){
				System.err.println("Caught IOException: " + e.getMessage());
				JOptionPane.showMessageDialog(configFrame, "There are no saved games.  Begin a new game!");
				welPanel.setVisible(false);	
				configFrame.setVisible(true);
			}
		}
	}
	
	private class NewListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			welPanel.setVisible(false);	
			configFrame.setVisible(true);
		}
	}
}
